﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dao
{
    public class RolePermissions_Dao
    {
        /// <summary>
        /// 新增一条记录
        /// </summary>
        /// <param name="entity">记录实体</param>
        /// <returns>操作影响的行数</returns>
        public int Add(RolePermission entity)
        {
            using (var context = new Dao.ErpEntities())
            {
                context.RolePermissionSet.AddObject(entity);
                return context.SaveChanges();
            }
        }

        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="id">记录Id</param>
        /// <returns>操作影响的行数</returns>
        public int Delete(string roleId, string permissionId)
        {
            using (var context = new Dao.ErpEntities())
            {
                Guid roleid = Guid.Parse(roleId);
                Guid perid = Guid.Parse(permissionId);
                RolePermission entity = context.RolePermissionSet.FirstOrDefault(c => c.RoleId == roleid && c.PermissionId == perid);
                if (entity != null)
                {
                    context.DeleteObject(entity);
                    return context.SaveChanges();
                }
                return 0;
            }
        }

        public List<RolePermission> GetByRole(string roleId)
        {
            List<RolePermission> list = new List<RolePermission>();
            using (var context = new Dao.ErpEntities())
            {
                Guid guid = Guid.Parse(roleId);
                var q = from rp in context.RolePermissionSet
                        where rp.RoleId == guid
                        select rp;
                foreach (var c in q)
                {
                    list.Add(c);
                }
                return list;
            }
        }

        /// <summary>
        /// 查询一条记录
        /// </summary>
        /// <param name="id">记录Id</param>
        /// <returns>查询结果实体</returns>
        public RolePermission Query(string roleId, string permissionId)
        {
            using (var context = new Dao.ErpEntities())
            {
                Guid roleid = Guid.Parse(roleId);
                Guid perid = Guid.Parse(permissionId);
                RolePermission entity = context.RolePermissionSet.FirstOrDefault(c => c.RoleId == roleid && c.PermissionId == perid);
                return entity;
            }
        }

        /// <summary>
        /// 查询所有记录
        /// </summary>
        /// <returns>查询结果集</returns>
        public List<RolePermission> QueryAll()
        {
            List<RolePermission> list = new List<RolePermission>();
            using (var context = new Dao.ErpEntities())
            {
                var q = from c in context.RolePermissionSet
                        select c;
                foreach (var c in q)
                {
                    list.Add(c);
                }
                return list;
            }
        }
    }
}
